#include <stdio.h>
#include <stdlib.h>


void swap_data(double *d1,double *d2)
{
    double temp;
    temp = *d1;
    *d1 = *d2;
    *d2 = temp;
}

void flip_data(double *odata,int nfft,long size)
{
    for(int i = 0;i < size / nfft;++i){
        for(int j = 0;j < nfft / 2;++j){
            // printf("%d\n",i);
            swap_data(&odata[i*nfft+j],&odata[i*nfft+nfft-j-1]);
        }
    }
}
void opfb_data(double *data,double *odata,double *half_data,int nfft,long block_size)
{
    int half = nfft / 2;
    for(int i = 0;i < half;++i)
        odata[i] = half_data[i];
    for(int i = 0;i < block_size / half;++i){
        for(int j = 0;j < half;++j){
            odata[i*nfft+j+half]      = data[i*half+j];
            if(i < block_size / half-1){
                odata[i*nfft+j+half+half] = data[i*half+j];
            }
        }
    }
    for(int i = 0;i < half;++i)
        half_data[i] = data[block_size - half + i];

    flip_data(odata,nfft,2*block_size);
}

int main()
{
    double data[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    double half_data[4] = {0,0,0,0};
    double *odata = malloc(sizeof(double) * 32);
    opfb_data(data,odata,half_data,8,16);

    for(int i = 0;i < 32;++i){
        printf("%lf ",odata[i]);
    }
    printf("\n");
    free(odata);
    return 0;
}